package com.example.controller;

import com.example.entity.po.Course;
import com.example.entity.po.Primary;
import com.example.utils.ResultUtils;
import com.example.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("courseList")
    public ResultUtils courseList(){
        List<Course> list=courseService.queryCourse();
        if (list.size()==0){
            return ResultUtils.Failure("查询课程信息失败",null);
        }else {
            return ResultUtils.Successful("查询课程信息成功",list);
        }
    }

    @PostMapping("/updateCourse")
    public ResultUtils updateCourse(@RequestBody Course course){
        int res=courseService.updateCourse(course);
        if (res==0){
            return ResultUtils.Failure("修改课程信息失败",null);
        }else {
            return ResultUtils.Successful("修改课程信息成功",null);
        }
    }

    @PostMapping("/deleteCourse")
    public ResultUtils deleteCourse(String classId){
        int res=courseService.deleteCourse(classId);
        if (res==0){
            return ResultUtils.Failure("删除课程信息失败",null);
        }else {
            return ResultUtils.Successful("删除课程信息成功",null);
        }
    }

    @PostMapping("/addCourse")
    public ResultUtils addCourse(@RequestBody Course course){
        int res=courseService.addCourse(course);
        if (res==0){
            return ResultUtils.Failure("添加课程信息失败",null);
        }else {
            return ResultUtils.Successful("添加课程信息成功",null);
        }
    }

    //对课程进行模糊查询
    @PostMapping("/fuzzyCourse")
    public ResultUtils fuzzyCourse(String className){
        List<Course> courseList= courseService.fuzzyCourse(className);
        if (courseList.size()==0){
            return ResultUtils.Failure("搜索课程成功或无该课程",null);
        }else {
            return ResultUtils.Successful("搜索课程成功",courseList);
        }
    }
}
